package com.cathyjava.easyflows.workflow;

import com.cathyjava.easyflows.work.Work;
import com.cathyjava.easyflows.work.WorkContext;
import com.cathyjava.easyflows.work.WorkReport;

import java.util.function.Predicate;

/**
 * @author chenjing
 * @date 2021/5/7
 */
public class RepeatableWorkFlow extends AbstractWorkFlow {
    private Work work;
    private Predicate p;

    public RepeatableWorkFlow(String name, Work work, Predicate p) {
        super(name);
        this.work = work;
        this.p = p;
    }

    @Override
    public WorkReport execute(WorkContext workContext) {
        WorkReport workReport;
        do {
            workReport = this.work.execute(workContext);
        } while (this.p.test(workContext));

        return workReport;
    }
}
